package com.tc.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.tc.model.dto.UpdateUserInfoDTO;
import com.tc.model.vo.LoginReqVO;
import com.tc.model.vo.UpdatePasswordReqVO;
import com.tc.operationlog.aspect.ApiOperationLog;
import com.tc.common.response.Response;
import com.tc.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户请求登录接口
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperationLog(description = "登录接口")
    public Response<?> login(@RequestBody @Validated LoginReqVO loginDTO){
        return userService.loginAndRegister(loginDTO);
    }

    /**
     * 查询用户信息接口
     * @return
     */
    @PostMapping("/info")
    @ApiOperationLog(description = "查询用户信息接口")
    public Response<?> info(){
        return userService.findCurrentUserInfo();
    }

    /**
     * 更新用户接口
     * @return
     */
    @PostMapping("/update")
    @ApiOperationLog(description = "更新用户接口")
    public Response<?> update(@RequestBody UpdateUserInfoDTO updateUserInfoDTO){
        return userService.updateUserInfoByUserId(updateUserInfoDTO);
    }

    /**
     * 用户退出登录接口
     * @return
     */
    @PostMapping("/logout")
    @ApiOperationLog(description = "登出接口")
    public Response<?> logot(){
        return userService.logout();
    }

    @PostMapping("/isLogin")
    @ApiOperationLog(description = "用户是否登录接口")
    public Response<?> isLogin(){
        return Response.success(StpUtil.isLogin());
    }


    @PostMapping("/password/update")
    @ApiOperationLog(description = "用户修改密码")
    public Response<?> updatePassword(@Validated @RequestBody UpdatePasswordReqVO updatePasswordReqVO){
        return userService.updatePassword(updatePasswordReqVO);
    }
}

